package common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.dao.JdbcDAO;
import common.pojo.MemberOtherInfo;

@Scope("prototype")
@Repository
public class MemberOtherInfoDAO  extends JdbcDAO {
	public boolean delete(int id){
		String sql ="delete from memberotherinfo where memberid = "+id;
		delete(sql);
		return true;
	}
	
	public void save(MemberOtherInfo memberOtherInfo){
		String sql="insert into memberOtherInfo values(?,getdate(),?,getdate(),?)";
		save(sql, new Object[]{ memberOtherInfo.getMemberId(),memberOtherInfo.getRegIp(),memberOtherInfo.getLoginIp()});
	}
	
	public void saveback(MemberOtherInfo memberOtherInfo){
		String sql="insert into memberOtherInfo(memberId,regIp,regTime) values(?,?,getdate())";
		save(sql, new Object[]{ memberOtherInfo.getMemberId(),memberOtherInfo.getRegIp()});
	}
	
	public MemberOtherInfo findInfoById(int id) {
		class MOtherInfoMapper implements org.springframework.jdbc.core.RowMapper {

			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				MemberOtherInfo memberOtherInfo = new MemberOtherInfo();
				memberOtherInfo.setMemberId(rs.getInt(1));
				memberOtherInfo.setRegTime(rs.getTimestamp(2));
				memberOtherInfo.setRegIp(rs.getString(3));
				memberOtherInfo.setLastLoginTime(rs.getTimestamp(4));
				memberOtherInfo.setLoginIp(rs.getString(5));
				return memberOtherInfo;
			}
		}
		String sql = "select * from memberOtherInfo where memberId ="+ id;
 		return (MemberOtherInfo) getObject(sql, null, new MOtherInfoMapper());
	}
	
	/**
	 * 更新数据库
	 * 
	 * @param 
	 * @return
	 */
	public boolean update(MemberOtherInfo memberOtherInfo) {
		
			String sql = "update memberOtherInfo set lastLoginTime=?, loginIp=? where memberId =?";
			update(sql, new Object[] {
					memberOtherInfo.getLastLoginTime(), memberOtherInfo.getLoginIp(),memberOtherInfo.getMemberId()});
	
		return true;
	}
	
	public boolean update(String ip,int id) {
		String sql = "update memberOtherInfo set lastLoginTime=getDate(),loginIp='"+ip+"' where memberId="+id;
		update(sql);
		return true;
	}
}


